package com.cn.sys.frame.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cn.sys.frame.dao.BaseSpringJdbcDao;
import com.cn.sys.frame.service.BaseService;
import com.cn.sys.frame.util.page.PageData;
import com.cn.sys.frame.util.query.QueryCondition;

public abstract class BaseServiceImpl<T, PK> implements BaseService<T, PK>{

	private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);
	
	protected BaseSpringJdbcDao<T, PK> baseDao;
	
	public abstract void setBaseDao(BaseSpringJdbcDao<T, PK> baseDao);
	
	public T get(PK id) throws Exception {
		T object = null;
		try {
			object = baseDao.get(id);
		} catch (Exception e) {
			//e.printStackTrace();
			logger.error("根据["+id+"]获取数据出错! ["+e.getMessage()+"]");
		}
		return object;
	}

	public T save(T entity) throws Exception {
		try {
			baseDao.save(entity);
		} catch (Exception e) {
			e.printStackTrace();
			entity = null;
		}
		return entity;
	}

	public T saveWithOutPK(T entity) throws Exception{
		try {
			baseDao.saveWithOutPK(entity);
		} catch (Exception e) {
			e.printStackTrace();
			entity = null;
		}
		return entity;
	}
	
	/*public int batchSave(Collection<T> entitys) throws Exception {
		int result = 0;
		try {
			result = baseDao.batchSave(entitys);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}*/

	public T update(T entity) throws Exception {
		try {
			baseDao.update(entity);
		} catch (Exception e) {
			e.printStackTrace();
			entity = null;
		}
		return entity;
	}

	//public int batchUpdate(Collection<T> entitys) throws Exception {
	//	return baseDao.batchUpdate(entitys);
	//}

	public int delete(PK id) throws Exception {
		int result = 0;
		try {
			result = baseDao.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public long getCount(QueryCondition queryCondition) throws Exception{
		return baseDao.getCount(null, queryCondition);
	}
	public List<T> getAll() throws Exception {
		return baseDao.listAll();
	}

	public PageData<T> getPageData(PageData<T> pageData,
			QueryCondition queryCondition) throws Exception {
		return baseDao.getPageData(pageData, queryCondition);
	}

	public PageData<Map<String, Object>> getPageMapData(PageData<Map<String, Object>> pageData,
			QueryCondition queryCondition) throws Exception{
		return null;
	}
}
